package com.iyao_app.adapter;

import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.entry.DemoData;
import com.iyao_app.util.ImageData;
import com.iyao_app.util.ResourcesUtils;
import com.iyaowoqu.base_library.util.ImageUtils;
import com.iyaowoqu.base_library.util.LogUtil;
import com.iyaowoqu.base_library.util.ScreenUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/21.
 */

public class PersonInfoAdapter extends BaseQuickAdapter<String, BaseViewHolder> {


    public PersonInfoAdapter(@Nullable List<String> data) {
        super(R.layout.item_person_info, data);

    }

    @Override
    protected void convert(final BaseViewHolder helper, String item) {
        final int position = helper.getLayoutPosition();
        SimpleDraweeView sdvPhoto = helper.getView(R.id.sdv_photo);
        int size = (int) ResourcesUtils.getDimension(R.dimen.x181);
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(size, size);
        sdvPhoto.setLayoutParams(params);

        if ("mipmap" .equals(item)) {
            sdvPhoto.setImageResource(R.mipmap.btn_tianjia);
        } else if (!item.startsWith("http")) {
            try {
                Uri uri = Uri.parse(ImageUtils.IMAGE_FILE + item);
                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions(200, 200))
                        .build();
                PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                        .setOldController(sdvPhoto.getController())
                        .setImageRequest(request)
                        .build();
                sdvPhoto.setController(controller);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            try {
                Uri uri = Uri.parse(item);
                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions(200, 200))
                        .build();
                PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                        .setOldController(sdvPhoto.getController())
                        .setImageRequest(request)
                        .build();
                sdvPhoto.setController(controller);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.sdv_photo)
        SimpleDraweeView sdvPhoto;


        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    PictureSelectListener selectListener;

    public void setOnPictureSelectListener(PictureSelectListener onPictureSelectListener) {
        selectListener = onPictureSelectListener;
    }

    public interface PictureSelectListener {
        void onSelect(int position);
    }
}
